package cn.datawin.routeserver.input.service;

import cn.datawin.bms.customer.entity.CstmCustomerEntity;
import cn.datawin.routeserver.input.machine.Basket;
import cn.datawin.routeserver.input.machine.InputDealMachine;
import cn.datawin.routeserver.service.CstmCustomerService;
import cn.datawin.routeserver.service.SmsDetailLogService;
import cn.datawin.sms.cmpp2.SmsDetailLog;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;

import static cn.datawin.common.state.StateMachineUtil.createAndStartMachine;
import static cn.datawin.common.state.constant.State.INIT;
import static cn.datawin.routeserver.input.machine.InputState.*;


@Slf4j
@Service
@RequiredArgsConstructor
public class InputDealMachineService {
	private final CstmCustomerService cstmCustomerService;


    /**
     * 状态机方式处理输入消息
     */

    public void dealInputLog(String msg) {
        try {
			SmsDetailLog smsDetailLog = msg2SmsDetailLogAndSave(msg);
//            List<SimpleTemplate> simpleTemplateList = new ArrayList<>();
//            simpleTemplateList.add(new SimpleTemplate(InputState.CHECK, new CheckAction()));
//            simpleTemplateList.add(new SimpleTemplate(InputState.FILTER, new FilterAction()));
//            simpleTemplateList.add(new SimpleTemplate(InputState.SUBMIT, new SubmitAction()));
//            UntypedStateMachineBuilder builder = StateMachineUtil.initBuilder(InputDealMachine.class, simpleTemplateList);
//            UntypedStateMachine fsm = builder.newStateMachine(INIT);
//            Basket basket = new Basket();
//            List<String> collect = simpleTemplateList.stream().map(SimpleTemplate::getStateName).collect(Collectors.toList());
//            basket.setLines(collect);
//            fsm.start();
//            fsm.fire(event(INIT, collect.get(0)), basket);
//            fsm.terminate();
			//缓存对象
			Basket basket = new Basket();
			basket.setSmsDetailLog(smsDetailLog);
			//执行状态机逻辑
			//check 检测
			//filter 过滤
			//submit 提交
			createAndStartMachine(InputDealMachine.class,INIT,basket,check(),filter(),submit());
		} catch (Exception e) {
            log.error("InputDealMachineService---exception",e);
        }

    }

    /**
     * 转换文本消息到SmsDetailLog
     * @param msg 消息内容
	 * @return
     */
    private SmsDetailLog msg2SmsDetailLogAndSave(String msg) {
		Date now = new Date();
		SmsDetailLog smsDetailLog = JSON.parseObject(msg, SmsDetailLog.class);
		//插入数据库
		String cstmCustomerCode = smsDetailLog.getCstmCustomerCode();
		if(StrUtil.isNotBlank(cstmCustomerCode)){
			//根据客户编码查询客户信息
			CstmCustomerEntity cstmCustomer = cstmCustomerService.getByCstmCustomerCode(cstmCustomerCode);
			if(cstmCustomer!=null){
				//设置客户id
				smsDetailLog.setCstmCustomerId(cstmCustomer.getId());
				smsDetailLog.setPrice(cstmCustomer.getPrice());
			}
		}

		//受理时间 (队列接收到消息)
		smsDetailLog.setAcceptTime(now);
		//设置拦截状态 0
		smsDetailLog.setInterruptStatus(0);
		//设置提交状态 0
		smsDetailLog.setSubmitStatus(0);
		//设置回执状态 0
		smsDetailLog.setReportStatus(0);
		//设置年月日
		smsDetailLog.setYmd(DateUtil.format(now,"yyyyMMdd"));

		//插入到数据库
		SpringUtil.getBean(SmsDetailLogService.class).save(smsDetailLog);


		return smsDetailLog;
    }



}
